package com.xiaoliang.servlet;

import com.xiaoliang.entry.User;
import com.xiaoliang.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author XIAOLIANG
 * @ClassName Register.java
 * @Description TODO
 * @createTime 2021年02月26日 15:16:00
 */

public class Register extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String username = req.getParameter("username");
		String password = req.getParameter("password");

		UserServiceImpl userService = new UserServiceImpl();

		boolean register= false;
		try {
			register = userService.register(new User(username, password));
		} catch (Exception e) {
			e.printStackTrace();
		}
		HttpSession session = req.getSession();
		session.setAttribute("username",username);
		if (register){
		session.setAttribute("msg","注册成功, 请登录!");
		resp.sendRedirect(req.getContextPath()+"/login.html");

		}else {
			session.setAttribute("msg","注册失败! 用户名重复");
			session.setAttribute("password",password);
			resp.sendRedirect(req.getContextPath()+"/regist.jsp");
		}


	}
}
